//
//  YQArticleDetailViewController.m
//  iyuanqi
//
//  Created by czl on 2017/7/28.
//  Copyright © 2017年 czl. All rights reserved.
//

#import "YQArticleDetailViewController.h"
#import "YQArticleDetailHeader.h"
#import "YQReplyCell.h"

@interface YQArticleDetailViewController ()
<UITableViewDelegate>

@property(nonatomic,copy)YQArticleObject* articleObject;

@property(nonatomic,strong)YQArticleDetailHeader* detailHeader;
@property(nonatomic,strong)UITableView* tableViewArticle;
@property(nonatomic,strong)NSMutableArray* arrayReplys;
@property(nonatomic,strong)ZLCellDataSource* dataSource;

@end

@implementation YQArticleDetailViewController

- (instancetype)initWithArticle:(YQArticleObject*)article {
    if (self == [super init]) {
        self.articleObject = article;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"帖子详情";
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didChangePreferredContentSize:) name:UIContentSizeCategoryDidChangeNotification object:nil];
    
    [self.view addSubview:self.tableViewArticle];
    [self.tableViewArticle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.view);
    }];
    self.tableViewArticle.tableHeaderView = self.detailHeader;
    WeakSelf;
    self.detailHeader.onLoadFinish = ^(CGFloat height) {
        weakSelf.detailHeader.height = height;
        weakSelf.tableViewArticle.tableHeaderView = weakSelf.detailHeader;
    };
    
    [self getArticleDetail];
    [self getReplyList];
}

- (void)getArticleDetail {
    [YQWebManager getArticleDetail:self.articleObject.tid withSuccessBlock:^(id response) {
        if ([response[@"code"]integerValue]==0) {
            if ([response[@"data"] isKindOfClass:[NSArray class]]) {
                NSArray* array = response[@"data"];
                if (array.count > 0) {
                    self.articleObject = [YQArticleObject mj_objectWithKeyValues:array[0]];
                    self.detailHeader.articleObject = self.articleObject;
                }
            }
        }
    } withFailureBlock:^(NSError *error) {
        
    }];
}

- (void)getReplyList {
    [YQWebManager getReplys:self.articleObject.tid withSuccessBlock:^(id response) {
        if ([response[@"code"]integerValue]==0) {
            [self.arrayReplys removeAllObjects];
            [self.arrayReplys addObjectsFromArray:[YQReplyObject mj_objectArrayWithKeyValuesArray:response[@"data"]]];
            [self.tableViewArticle reloadData];
        }
    } withFailureBlock:^(NSError *error) {
        
    }];
}

- (void)didChangePreferredContentSize:(NSNotification *)notification {
    [self.tableViewArticle reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


- (YQArticleDetailHeader *)detailHeader{
    if(!_detailHeader){
        _detailHeader = ({
            YQArticleDetailHeader * object = [[YQArticleDetailHeader alloc]initWithFrame:CGRectMake(0, 0, 0, 100)];
            object;
       });
    }
    return _detailHeader;
}

- (UITableView *)tableViewArticle{
    if(!_tableViewArticle){
        _tableViewArticle = ({
            UITableView * object = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
            object.dataSource = self.dataSource;
            object.delegate = self;
            object.hideExtraCell = YES;
            object.separatorStyle = UITableViewCellSeparatorStyleNone;
            [object commonSetTableView];
            [object registerClass:YQReplyCell.class forCellReuseIdentifier:@"YQReplyCell"];
            object;
       });
    }
    return _tableViewArticle;
}

- (NSMutableArray *)arrayReplys{
    if(!_arrayReplys){
        _arrayReplys = ({
            NSMutableArray * object = [[NSMutableArray alloc]init];
            object;
       });
    }
    return _arrayReplys;
}

- (ZLCellDataSource *)dataSource{
    if(!_dataSource){
        _dataSource = ({
            WeakSelf;
            ZLCellDataSource * object = [[ZLCellDataSource alloc]initWithItems:self.arrayReplys cellIdentifier:@"YQReplyCell" configureCellBlock:^(YQReplyCell* cell, YQReplyObject* item, NSIndexPath *indexPath) {
                cell.replyObject = item;
                
            }];
            object;
       });
    }
    return _dataSource;
}
@end
